import { IAction, TableProps } from "@scmt/common-components";
import { useRequest } from "@scmt/common-hooks";
import { useApi } from "@scmt/core-api";
import { useEditPackage } from "./editPackage";
import {
  QualityControlEntity,
  QualityControlQueryParams,
} from "@scmt/api-lis-quality-control";

export const usePackages = () => {
  const $api = useApi();
  const { editPackage } = useEditPackage();

  const queryPramas = reactive<QualityControlQueryParams>({
    pageNum: 1,
    pageSize: 20,
  });

  const { data, loadingMap, request } = useRequest(
    {
      tableData: async (q: any) => {
        const params = Object.assign({}, queryPramas, q);

        if (!params.deviceId) {
          return {
            items: [],
            pagination: {
              total: 0,
              pageNum: params.pageNum,
              pageSize: params.pageSize,
            },
          };
        }

        return $api.lis.qualityControl.query(params).then((res) => {
          const { items, pagination } = res;
          Object.assign(queryPramas, pagination);

          currentPackage.value =
            (currentPackage.value
              ? items.find((i) => i.id === currentPackage.value?.id)
              : items[0]) || null;

          return res;
        });
      },
    },
    {
      tableData: {
        items: [] as QualityControlEntity[],
        pagination: {
          total: 0,
          pageNum: queryPramas.pageNum,
          pageSize: queryPramas.pageSize,
        },
      },
    }
  );

  const { tableData } = toRefs(data);
  const currentPackage = ref<null | QualityControlEntity>(null);

  const tableColumns: TableProps["columns"] = [
    {
      prop: "concentration",
      label: "浓度",
      tooltip: true,
      render: ({ row }) => {
        let resText = "";
        if (row.concentration == 1) {
          resText = "高浓度";
        } else if (row.concentration == 2) {
          resText = "中浓度";
        } else if (row.concentration == 3) {
          resText = "低浓度";
        }
        return h("div", resText);
      },
    },
    {
      prop: "concentrationLevel",
      label: "浓度水平值",
      tooltip: true,
    },
    {
      prop: "materialName",
      label: "质控物名称",
      tooltip: true,
    },
    {
      prop: "batchNumber",
      label: "质控物批号",
      tooltip: true,
    },
    {
      prop: "periodEffect",
      label: "质控物期效",
      tooltip: true,
    },
    {
      prop: "sampleNumber",
      label: "质控标本号",
      tooltip: true,
    },
    {
      prop: "packageCode",
      label: "质控包编码",
      tooltip: true,
    },
    {
      prop: "packageName",
      label: "质控包名称",
      tooltip: true,
    },

    {
      prop: "packageManufacturer",
      label: "质控包厂商",
      tooltip: true,
    },
    {
      prop: "createTime",
      label: "创建日期",
      tooltip: true,
    },
    {
      label: "操作",
      fixed: "right",
      actions: [
        {
          label: "编辑",
          event: "edit",
          type: "primary",
        },
        {
          label: "复制",
          event: "destroy",
          type: "danger",
        },
        {
          label: "删除",
          event: "destroy",
          type: "danger",
        },
        {
          label: "质控",
          event: "destroy",
          type: "danger",
        },
      ],
    },
  ];

  const tableProps = computed(() => {
    const tableProps: TableProps = {
      loading: loadingMap.tableData,
      data: tableData.value.items,
      pagination: tableData.value.pagination,
      columns: tableColumns,
      onAct: onPageActions,
      onPaginate: getPackages,
      onRowClick: (row) => {
        currentPackage.value = row;
      },
      currentRow: currentPackage.value,
      highlightCurrentRow: true,
    };
    return tableProps;
  });

  const getPackages = (q?: any) => {
    request("tableData", q);
  };

  const onPageActions = (action: IAction, model?: QualityControlEntity) => {
    switch (action.event) {
      case "edit":
        editPackage(model!);
        break;
    }
  };

  return {
    tableProps,
    getPackages,
    currentPackage,
  };
};
